<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改记账详情')"/>
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-money-edit" th:object="${accountMoney}">
        <input name="id" th:field="*{id}" type="hidden">
        <div class="form-group" style="display: none">
            <label class="col-sm-3 control-label is-required">账本id：</label>

            <div class="col-sm-8">
                <input name="money" th:field="*{accountId}" class="form-control" type="text" required readonly="true">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">账户类型：</label>
            <div class="col-sm-8">
                <select name="classType" id="classType" class="form-control" data-placeholder="请选择账户类型" th:with="type=${@dict.getType('account_income_pay')}" required>
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{classType}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">所属分类</label>
            <div class="col-sm-8">
                <select class="form-control" data-placeholder="请选择分类" id="classId" name="classId"
                        required>
                    <option value="">请选择</option>
                    <option th:each="accountClass:${accountClasseList}"
                            th:value="${accountClass.id}"
                            th:text="${accountClass.className}"
                            th:selected="${accountClass.id}==${accountMoney.classId}"
                            th:disabled="${accountClass.classStatus == '1'}"
                    ></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">账单金额：</label>
            <div class="col-sm-8">
                <input name="money" th:field="*{money}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">账单日期：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <input name="moneyDate" th:value="${#dates.format(accountMoney.moneyDate, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" readonly="readonly">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">账单状态：</label>
            <div class="col-sm-8">
                <div class="radio-box">
                    <input name="status" title="正常" type="radio" value="0" th:checked="*{status == '0'} ? true : false">
                    <label value="0" th:text="显示"></label>
                </div>
                <div class="radio-box">
                    <input name="status" title="停用" type="radio" value="1" th:checked="*{status == '1'} ? true : false">
                    <label value="1" th:text="隐藏"></label>
                </div>
                <div class="radio-box">
                    <input name="status" title="隐藏，到期自动显示" type="radio" value="2" th:checked="*{status == '2'} ? true : false">
                    <label value="2" th:text="隐藏，到期自动显示"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">是否入账：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('account_is_Total')}">
                    <input type="radio" th:id="${'isTotal_' + dict.dictCode}" name="isTotal"
                           th:value="${dict.dictValue}" th:field="*{isTotal}" required>
                    <label th:for="${'isTotal_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 请在数据字典添加 字典类型：account_income_pay </span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea rows="8" name="moneyIntroduce" class="form-control">[[*{moneyIntroduce}]]</textarea>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js" />
<script th:inline="javascript">
    var prefix = ctx + "account/money";
    $("#form-money-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var data =  $('#form-money-edit').serializeArray();
            var type = [[${type}]];
            data.push({"name": "type", "value": type});
            $.operate.save(prefix + "/edit",data);
        }
    }

    $("input[name='moneyDate']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    /**
     * 金额校验 https://blog.csdn.net/wl_Honest/article/details/81629026
     */
    $("#money").on('input  propertychange',function(){
        //如果输入非数字，则替换为''
        this.value = this.value.replace(/[^\d\.]/g, '');
        //必须保证第一个为数字而不是.
        this.value = this.value.replace(/^\./g,'');
        //保证只有出现一个.而没有多个.
        this.value = this.value.replace(/\.{2,}/g,'.');
        //保证.只出现一次，而不能出现两次以上
        this.value = this.value.replace('.','$#$').replace(/\./g,'').replace('$#$','.');
        //只能输入两位小数
        this.value = this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
    })

    // 监听选择框的选中事件
    var selectBox = document.getElementById("classType");
    selectBox.addEventListener("change", function() {
        // 获取选中的值
        var classType = selectBox.value;
        $.ajax({
            type: "GET",
            url: prefix + "/accountClass",
            data: {
                "classType": classType
            },
            dataType: "json",
            success: function (result) {
                var html = "";
                $.each(result.rows, function (i, n) {
                    html += "<option value='" + n.id + "'>" + n.className + "</option>";
                });
                $("#classId").html(html);
            }
        });

    });

</script>
</body>
</html>
